#include "stdafx.h"
#include "ExrFileIO.h"
//#undef min
//#undef max
//#undef ASSERT
//#undef THROW

CExrFileIO::CExrFileIO(void)
{
}


CExrFileIO::~CExrFileIO(void)
{
}

void CExrFileIO::LoadExrFile(char* strFileName)
{
	if(strFileName == NULL)
		 return;

	Imf::RgbaInputFile file (strFileName);
	Imath::Box2i dw = file.dataWindow();
	m_iImgWidth = dw.max.x - dw.min.x + 1;
	m_iImgHeight = dw.max.y - dw.min.y + 1;
	m_arrPixels.resizeErase (m_iImgWidth, m_iImgHeight);
	file.setFrameBuffer (&m_arrPixels[0][0], 1, m_iImgWidth);
	file.readPixels (dw.min.y, dw.max.y);

}

void CExrFileIO::SaveExrFile(char* strFileName, Imf::RgbaChannels iChannels /* = WRITE_RGB */)
{
	if(strFileName == NULL || m_iImgWidth == 0 || m_iImgHeight == 0)
		return;
	Imf::RgbaOutputFile outFile(strFileName, m_iImgWidth, m_iImgHeight, iChannels);
	outFile.setFrameBuffer (&m_arrPixels[0][0], 1, m_iImgWidth);
	outFile.writePixels (m_iImgHeight);
}

void CExrFileIO::FillPixelsArray(const double* arrInput2dData, const unsigned int iBufWidth, const unsigned int iBufHeight, const double fScaleRange, bool bColor/*=true*/)
{
	if(!arrInput2dData)
		return;

	m_arrPixels.resizeErase(iBufWidth, iBufHeight);
	m_iImgWidth = iBufWidth;
	m_iImgHeight = iBufHeight;

	if(bColor){
		for (unsigned int iY = 0; iY < iBufHeight; iY++)
			for (unsigned int iX = 0; iX < iBufWidth; iX++)
			{
				int iIdx = iY*iBufWidth + iX;
				double val = arrInput2dData[iIdx]/fScaleRange;//scale
				D3DXVECTOR3 vColor = CD3DUtilityFunction::GetColour(val,0.0f,1.0f);
				*(&m_arrPixels[0][0] + iIdx) = Imf::Rgba(static_cast<half>(vColor.x),static_cast<half>(vColor.y),static_cast<half>(vColor.z));
			}
	} else {
	//
	for (unsigned int iY = 0; iY < iBufHeight; iY++)
		for (unsigned int iX = 0; iX < iBufWidth; iX++)
		{
			int iIdx = iY*iBufWidth + iX;
			double val = arrInput2dData[iIdx]/fScaleRange;//scale
			*(&m_arrPixels[0][0] + iIdx) = Imf::Rgba(static_cast<half>(val),static_cast<half>(val),static_cast<half>(val));
		}
	}
}

//void CExrFileIO::SaveWarpedExrFile(char* strFileName, TransFormData_2D& trans, RgbaChannels iChannels /* = WRITE_RGB */)
//{
//	if(strFileName == NULL || m_iImgWidth == 0 || m_iImgHeight == 0)
//		return;
//
//	Array2D<Rgba> warpedImg;
//	warpedImg.resizeErase(m_iImgWidth,m_iImgHeight);
//	
//	//start warping
//	for( int iY=0; iY < m_iImgHeight; iY++ ) { 
//		for( int iX=0; iX < m_iImgWidth; iX++ ) {
//
//			float xPos = iX + trans.m_fTransX + 0.5f;
//			float yPos = iY + trans.m_fTransY + 0.5f;
//
//			if(xPos < 0 || yPos < 0 || xPos >= m_iImgWidth || yPos >= m_iImgHeight )
//				continue;
//
//			int xIdx = (int)(floorf(xPos));
//			int yIdx = (int)(floorf(yPos));
//
//    		*(&warpedImg[0][0] + yIdx*m_iImgWidth + xIdx) = 
//				*(&m_arrPixels[0][0] + iY*m_iImgWidth + iX); 
//		} //Set red to max (BGR format)
//	}
//
//	RgbaOutputFile outFile(strFileName, m_iImgWidth, m_iImgHeight, iChannels);
//	//RgbaOutputFile outFile(strFileName, m_iImgWidth, m_iImgHeight, iChannels,1,Imath::V2f(0, 0),1,INCREASING_Y,NO_COMPRESSION);
//	outFile.setFrameBuffer (&warpedImg[0][0], 1, m_iImgWidth);
//	outFile.writePixels (m_iImgHeight);
//}